package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 订单系统角色与菜单对应关系,对应order_role_menu表
 * @author zhoutian   
 * @date 2015-11-26
 */
public class SysRoleMenu {
	//角色ID
	private String roleId;
	//角色名称
	private String roleName;
	//菜单ID
	private String menuId;
	//菜单名称
	private String menuName;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;
	//有效标识,0:无效 1:有效
	private int isValid;

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public int getIsValid() {
		return isValid;
	}

	public void setIsValid(int isValid) {
		this.isValid = isValid;
	}

	public SysRoleMenu() {}
	
	private static final String select_all_sql = "select order_role_menu.role_id as roleId, pub_role.role_name as roleName, order_role_menu.menu_id as menuId, order_menu.menu_name as menuName, order_role_menu.is_valid as isValid, order_role_menu.creator, order_role_menu.createTime"
			+ " from order_role_menu inner join pub_role on order_role_menu.role_id = pub_role.role_id"
			+ " inner join order_menu on order_role_menu.menu_id = order_menu.menu_id";
	
	private static final String update_all_sql = "update order_role_menu set role_id = :roleId, menu_id = :menuId, is_valid = :isValid, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(isRoleIdAndMenuIdExist(this.roleId, this.menuId)){
			throw new BusinessException("选择的角色和菜单已存在");
		}
		String sql = "insert into order_role_menu(role_id, menu_id, is_valid, creator, createTime) values(:roleId, :menuId, :isValid, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update(){
		String sql = update_all_sql + " where role_id = :roleId and menu_id = :menuId";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from order_role_menu where role_id = :role_id and menu_id = :menu_id";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("role_id", this.roleId);
		paramMap.put("menu_id", this.menuId);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<SysRoleMenu> getAll(){
		String sql = select_all_sql + " order by cast (order_role_menu.menu_id as numeric) asc";
		return BaseDAOUtil.queryForList(sql, new HashMap(), SysRoleMenu.class);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static SysRoleMenu getBy(String roleId, String menuId){
		String sql = select_all_sql + " where order_role_menu.role_id = :roleId and order_role_menu.menu_id = :menuId";
		Map paramMap = new HashMap();
		paramMap.put("roleId", roleId);
		paramMap.put("menuId", menuId);
		return BaseDAOUtil.queryForObject(sql, paramMap, SysRoleMenu.class);
	}
	
	public static boolean isRoleIdAndMenuIdExist(String role_id, String menuId){
		SysRoleMenu sr = getBy(role_id, menuId);
		if(sr != null){
			return true;
		}
		return false;
	}
}
